package com.alex.util.http;

import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * @author alex
 */
@Component
@EnableScheduling
public class PoolingHttpClientTask {
    @Autowired
    private PoolingHttpClientConnectionManager poolingHttpClientConnectionManager;

    /**
     * 定时清理
     */
    @Scheduled(fixedDelay = 3000)
    public void idle_con_release() {
        if (poolingHttpClientConnectionManager != null) {
            poolingHttpClientConnectionManager.closeExpiredConnections();
            poolingHttpClientConnectionManager.closeIdleConnections(30, TimeUnit.SECONDS);
        }
    }
}
